<template>
  <div class="global-container">
    <Atable :tableData="tableData" :tableModel="tableModel">
      <template #down="{ row }">
        <i
          class="el-icon-s-order"
          style="font-size: 24px"
          @click="down(row)"
        ></i>
      </template>
      <template #actions="{ row }">
        <el-button
          v-if="row.showBtn"
          @click="apply(row)"
          type="primary"
          size="small"
          >申请</el-button
        >
      </template>
    </Atable>
  </div>
</template>
<script>
import Atable from '@/components/Table/Atable';
import businessApi from '@/api/business';
import userApi from '@/api/user';
import { decrypt } from '@/utils/sms4util';

export default {
  components: { Atable },
  data() {
    return {
      searchData: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
      tableData: [],
      // table fields
      tableModel: [
        {
          prop: 'code',
          label: '事项编号',
        },
        {
          prop: 'name',
          label: '事项名称',
          width: '300px',
        },
        {
          prop: 'down',
          label: '服务指南',
          slot: 'down',
        },
        {
          prop: 'actions',
          label: '操作',
          slot: 'actions',
        },
      ],
      userData: {}
    };
  },
  computed: {
    userId() {
      return this.$store.state.user.userInfo.id;
    },
    userType() {
      return this.$store.state.user.userInfo.userType;
    },
  },
  created() {
    this.getUserInfo()
    this.getTable();
  },
  methods: {
    async getUserInfo() {
      let params = {
        id: this.userId,
        type: this.userType,
      };
      const res = await userApi.getUserInfo(params);
      this.userData = {
        ...res,
        phone: res.phone ? decrypt(res.phone) : '',
        paperCode: res.paperCode ? decrypt(res.paperCode) : '',
      };
    },
    async getTable() {
      let params = {
        ...this.searchData,
        status: '0,1,3,9',
      };
      const { records } = await businessApi.getStepsList(params);
      let params2 = {
        businessTable: 'SYS_FILE_RULE',
        businessId: 'bussType1-4',
      };
      const res = await businessApi.getFileList(params2);
      let fileObj = {};
      res.forEach((item) => {
        fileObj[item.subType] = item;
      });
      let arr = [
        {
          code: '500001',
          name: '二级造价工程师（水利工程）初始注册',
          bussType: '1',
          file: fileObj['500001'],
          showBtn: true,
        },
        {
          code: '500002',
          name: '二级造价工程师（水利工程）延续注册',
          bussType: '2',
          file: fileObj['500002'],
          showBtn: true,
        },
        {
          code: '500003',
          name: '二级造价工程师（水利工程）变更注册',
          bussType: '3',
          file: fileObj['500003'],
          showBtn: true,
        },
        {
          code: '500004',
          name: '二级造价工程师（水利工程）注销注册',
          bussType: '4',
          file: fileObj['500004'],
          showBtn: true,
        },
      ];
      records.forEach((item) => {
        if (item.bussType) {
          arr.forEach((i, idx) => {
            if (item.bussType == i.bussType) {
              arr[idx].showBtn = false;
            }
          });
        }
      });
      this.tableData = arr;
    },
    // 查看手册
    down(row) {
      window.open(row.file.fileUri);
    },
    // 申请
    apply(row) {
      // 未完善个人信息
      if (this.$store.state.user.userInfo.isCompleteInfo != 1) {
        this.$message.warning('请先完善个人信息');
        this.$router.push('/userInfo');
        return false;
      }
      // 已有注册证书
      if(row.bussType == 1 && this.userData.regCert){
        this.$notify({
          title: '提示',
          message: '已有注册证书',
          type: 'warning'
        });
        return false
      }
      // 未进行初始注册
      if(row.bussType != 1 && !this.userData.regCert){
        this.$notify({
          title: '提示',
          message: '请先进行初始注册',
          type: 'warning'
        });
        return false
      }
      // 进行申请
      this.$router.push({
        path: '/personalApplyStep',
        query: {
          bussType: row.bussType,
        },
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
